package Easy;

import java.util.Stack;
// 20.有效的括号
public class Solution20 {
    public boolean isValid(String s) {
        Stack stack = new Stack();
        for(int i = 0;i<s.length();i++){
            try {
                switch (s.charAt(i)) {
                    case ')':
                        if ((char) stack.pop() != '(')
                            return false;
                        break;
                    case ']':
                        if ((char) stack.pop() != '[')
                            return false;
                        break;
                    case '}':
                        if ((char) stack.pop() != '{')
                            return false;
                        break;
                    default:
                        stack.push(s.charAt(i));
                }
            }catch (Exception e){
                return false;
            }
        }
        if(stack.empty())
            return true;
        return false;
    }
}
